package com.guojian.jdbc.sql;

import java.util.Optional;

import org.springframework.util.Assert;

import com.guojian.jdbc.DBType;

import lombok.extern.slf4j.Slf4j;

/**
 * @author GuoJian
 * 
 * 
 * Sql属性配置类
 *
 */
@Slf4j
public class SqlContext {

	private static SqlParserProvider sqlParserProvider;

	static {
		try {
			Class.forName("com.github.pagehelper.parser.CountSqlParser");
			sqlParserProvider = new PagehelperSqlParserProvider();
		} catch (ClassNotFoundException e) {
			try {
				Class.forName("com.alibaba.druid.sql.PagerUtils");
				sqlParserProvider = new DruidSqlParserProvider();
			} catch (ClassNotFoundException e1) {
				//throw new RuntimeException(" sqlParserProvider not found ");
				log.info("sqlParserProvider not found, [pagehelper] ? or [druid] ?", e1);
			}

		}
	}

	public static SqlParserProvider getDruidSqlParserProvider() {
		return new DruidSqlParserProvider();
	}

	public static SqlParserProvider getPagehelperSqlParserProvider() {
		return new PagehelperSqlParserProvider();
	}

	public static SqlParserProvider getCurrentSqlParserProvider() {
		Assert.notNull(sqlParserProvider, "sqlParserProvider不能为空");
		return sqlParserProvider;
	}

	public static SqlParser getSqlParser(DBType dbType) {
		return getCurrentSqlParserProvider().getSqlParser(dbType);
	}

	public static void main(String[] args) {
		System.out.println(getCurrentSqlParserProvider());
		System.out.println(getCurrentSqlParserProvider().getProviderName());

		String sql = "select USER_ID from SYS_USER  ORDER BY user_id";
		System.out.println(getPagehelperSqlParserProvider().getSqlParser(DBType.oracle).getPageSql(sql, 1, 10));
	}

}
